package reservice;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URL;
import java.nio.charset.Charset;

import org.jsoup.Jsoup;
import org.jsoup.nodes.*;
import org.jsoup.select.*;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;

import controller.Domain;
import controller.ReDomain;

import java.util.*;

public class AlexaUpdate extends Thread{
	ReDomain domain;
	public AlexaUpdate(ReDomain domain)
	{
		this.domain=domain;
		this.start();
	}
	public void run()
	{
		try {
			Random random = new Random();
			
//			Socket sockket = new Socket("alexa.com", 80, InetAddress.getByName("67.205.109.38"), random.nextInt(1000)+30000);
			Socket sockket = new Socket("www.alexa.com", 80);
			BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(sockket.getOutputStream()));
			
			writer.write("GET /siteinfo/"+domain.getName()+" HTTP/1.1\r\n");
			writer.write("Host: www.alexa.com\r\n");
			writer.write("Referer: http://5ty.org\r\n");
			
			writer.write("User-Agent:	Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0.1) Gecko/20100101 Firefox/8.0.1\r\n");
			writer.write("Connection:	Close\r\n");
			
			
			writer.write("\r\n\r\n");
			writer.flush();
			BufferedReader reader =  new BufferedReader(new InputStreamReader(sockket.getInputStream()));
			String content = "";
			char[] buf = new char[102102];
			int numRead=0;
	        while((numRead=reader.read(buf)) != -1){
	            String readData = String.valueOf(buf, 0, numRead);
	            content += readData;
	            buf = new char[102102];
	        }
	        reader.close();
	
			Document doc = Jsoup.parse(content);
			System.out.println(doc.select("#siteStats").select("div").first().text().trim().replace(",",""));
			// visitor
			String visit = "0";
			if(doc.select(".visitors_percent").select(".avg").size() > 0)
			{
				visit = doc.select(".visitors_percent").select(".avg").last().text().trim();
			}
			// rank
			String ranks = null;
			if(doc.select("#siteStats").select("div").size() > 0)
			{
				ranks = doc.select("#siteStats").select("div").first().text().trim().replace(",","");
			}
			String rank = "0";
			
			try {
				if(ranks != null && !ranks.equals(""))
				{
					rank= ranks;
				}
			} catch (Exception e) {
				// TODO: handle exception
			}

			String keywords = "";
			Elements elements = doc.select("#top-keywords-from-search");
			
			if(elements.size() > 0)
			{
				for (Element element : elements.first().select("u")) {
					keywords += element.text()+" | ";
				}
			}
			
			elements = doc.select("#siteSentences + div  ").select("a");
			
			String category= "";
			for (Element element : elements) {
				category += element.text()+" | ";
			}
			domain.setCategory(category);
			domain.setPopularKeyword(keywords);
			domain.setAlexa(rank);

			
		} catch (Exception e) {
			
			e.printStackTrace();
		}
	}
	public static void main( String args[])
	{
		
//		Domain domain = new Domain();
//		domain.setName("google.com");
//		new AlexaUpdate(domain);
	}
	
}
